{# The url of the current release, `/crate/:name/:version` #}
{%- import "macros.html" as macros -%}

{%- include "header/topbar_begin.html" -%}
<ul class="pure-menu-list">
    <script id="crate-metadata" type="application/json">
        {# the only text that needs escaping in a <script> is `</`, none of the
            values below can include that sequence #}
        {
            "name": {{ metadata.name|json_encode }},
            "version": {{ metadata.version|json_encode }}
        }
    </script>
    {%- if krate is defined -%}
        <li class="pure-menu-item pure-menu-has-children">
            <a href="#" class="pure-menu-link crate-name" title="{{ krate.description.as_deref().unwrap_or_default() }}">
                {{ crate::icons::IconCube.render_solid(false, false, "") }}
                <span class="title">{{ krate.name }}-{{ krate.version }}</span>
            </a>

            {#- Crate details -#}
            <div class="pure-menu-children package-details-menu">
                {# Crate name, description and license #}
                <ul class="pure-menu-list menu-item-divided">
                    <li class="pure-menu-heading" id="crate-title">
                        {{ krate.name }} {{ krate.version }}
                        <span id="clipboard" class="svg-clipboard" title="Copy crate name and version information"></span>
                    </li>

                    {%- if metadata.req_version.to_string() == "latest" -%}
                    <li class="pure-menu-item">
                        <a href="{% if permalink_path is defined %}{{permalink_path|safe}}{% endif %}" class="pure-menu-link description" id="permalink" title="Get a link to this specific version">
                            {{- crate::icons::IconLink.render_solid(false, false, "") }} Permalink
                        </a>
                    </li>
                    {%- endif -%}

                    <li class="pure-menu-item">
                        <a href="{{ params.crate_details_url() }}" class="pure-menu-link description" title="See {{ krate.name }} in docs.rs">
                            {{ crate::icons::IconCube.render_solid(false, false, "") }} Docs.rs crate page
                        </a>
                    </li>

                    {%- if let Some(parsed_licenses) = krate.parsed_license -%}
                        <li class="pure-menu-item">
                            <span class="pure-menu-link description">
                            {{- crate::icons::IconScaleUnbalancedFlip.render_solid(false, false, "") }}
                            {%+ for item in parsed_licenses -%}
                                {%- match item -%}
                                    {%- when crate::web::licenses::LicenseSegment::Spdx(license) -%}
                                        <a href="https://spdx.org/licenses/{{ license|safe }}" class="pure-menu-sublink">{{ license }}</a>
                                    {%- when crate::web::licenses::LicenseSegment::UnknownLicense(license) -%}
                                        {{ license }}
                                    {%- when crate::web::licenses::LicenseSegment::GlueTokens(tokens) -%}
                                        {{ tokens }}
                                {%- endmatch -%}
                            {%- endfor -%}
                            </span>
                        </li>
                    {%- endif -%}
                </ul>

                <div class="pure-g menu-item-divided">
                    <div class="pure-u-1-2 right-border">
                        <ul class="pure-menu-list">
                            <li class="pure-menu-heading">Links</li>

                            {# If the crate has a homepage, show a link to it #}
                            {%- if let Some(homepage_url) = krate.homepage_url -%}
                                <li class="pure-menu-item">
                                    <a href="{{ homepage_url }}" class="pure-menu-link">
                                        {{ crate::icons::IconHouse.render_solid(false, false, "") }} Homepage
                                    </a>
                                </li>
                            {%- endif -%}

                            {# If the crate has external docs, show a link #}
                            {%- if let Some(documentation_url) = krate.documentation_url -%}
                                <li class="pure-menu-item">
                                    <a href="{{ documentation_url }}" title="Canonical documentation" class="pure-menu-link">
                                        {{ crate::icons::IconFileLines.render_regular(false, false, "") }} Documentation
                                    </a>
                                </li>
                            {%- endif -%}

                            {# If the crate has a repo url, show it #}
                            {%- if let Some(repository_url) = krate.repository_url -%}
                                <li class="pure-menu-item">
                                    <a href="{{ repository_url }}" class="pure-menu-link">
                                        {{ crate::icons::IconCodeBranch.render_solid(false, false, "") }} Repository
                                    </a>
                                </li>
                            {%- endif -%}

                            <li class="pure-menu-item">
                                <a href="https://crates.io/crates/{{ krate.name }}" class="pure-menu-link" title="See {{ krate.name }} in crates.io">
                                    {{ crate::icons::IconCube.render_solid(false, false, "") }} crates.io
                                </a>
                            </li>

                            {# A link to the release's source view #}
                            <li class="pure-menu-item">
                                <a href="{{ params.source_url() }}" title="Browse source of {{ metadata.name }}-{{ metadata.version }}" class="pure-menu-link">
                                    {{ crate::icons::IconFolderOpen.render_solid(false, false, "") }} Source
                                </a>
                            </li>
                        </ul>
                    </div>

                    {#- Show the crate owners -#}
                    <div class="pure-u-1-2">
                        <ul class="pure-menu-list" id="topbar-owners">
                            <li class="pure-menu-heading">Owners</li>

                            {%- for owner in krate.owners -%}
                                <li class="pure-menu-item">
                                    <a href="https://crates.io/{{ owner.2 }}s/{{ owner.0 }}" class="pure-menu-link">
                                        {{ crate::icons::IconUser.render_solid(false, false, "") }} {{ owner.0 }}
                                    </a>
                                </li>
                            {%- endfor -%}
                        </ul>
                    </div>
                </div>

                <div class="pure-g menu-item-divided">
                    <div class="pure-u-1-2 right-border">
                        <ul class="pure-menu-list">
                            <li class="pure-menu-heading">Dependencies</li>

                            {# Display all dependencies that the crate has #}
                            <li class="pure-menu-item">
                                <div class="pure-menu pure-menu-scrollable sub-menu" tabindex="-1">
                                    <ul class="pure-menu-list">
                                        {% call macros::dependencies_list(dependencies=krate.dependencies, use_crate_details = false, if_empty="") %}
                                    </ul>
                                </div>
                            </li>
                        </ul>
                    </div>

                    <div class="pure-u-1-2">
                        <ul class="pure-menu-list">
                            <li class="pure-menu-heading">Versions</li>

                            <li class="pure-menu-item">
                                <div class="pure-menu pure-menu-scrollable sub-menu" id="releases-list" tabindex="-1" data-url="{{ params.releases_partial_url() }}">
                                    <span class="rotate">{{ crate::icons::IconSpinner.render_solid(false, false, "") }}</span>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
                {%- if let (Some(documented), Some(total)) = (krate.documented_items, krate.total_items) -%}
                    {% set documented = documented as f32 %}
                    {% set total = total as f32 %}
                    {% set percent = documented * 100f32 / total %}
                    <div class="pure-g">
                        <div class="pure-u-1">
                            <ul class="pure-menu-list">
                                <li>
                                    <a href="{{ params.crate_details_url() }}" class="pure-menu-link">
                                        <b>{{ percent|round(2) }}%</b>
                                        of the crate is documented
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                {%- endif -%}
            </div>
        </li>
    {%- else -%}
        <li class="pure-menu-item">
            <a href="{{ params.crate_details_url() }}" class="pure-menu-link crate-name" {% if let Some(description) = metadata.description %}title="{{description}}"{% endif %}>
                {{ crate::icons::IconCube.render_solid(false, false, "") }}
                <span class="title">{{ metadata.name }}-{{ metadata.version }}</span>
            </a>
        </li>
    {%- endif -%}

    {#- If this is the latest release and it's been yanked, just display a warning -#}
    {%- set yanked = metadata.yanked.unwrap_or_default() -%}
    {%- if (is_latest_version is not defined || is_latest_version) && yanked -%}
        <li class="pure-menu-item">
            <span class="pure-menu-link warn">
                {{ crate::icons::IconTriangleExclamation.render_solid(false, false, "") }}
                <span class="title">This release has been yanked</span>
            </span>
        </li>

    {#- If this isn't the most recent stable release, offer a link to the latest -#}
    {%- elif is_latest_version is defined && !is_latest_version -%}
        {% set tooltip %}
        {% set title %}
        {%- if yanked -%}
            {%- set tooltip = "You are seeing a yanked version of the {} crate. Click here to go to the latest version."|format(metadata.name) -%}
            {%- set title = "This release has been yanked, go to latest version" -%}
        {%- elif is_prerelease is defined && is_prerelease -%}
            {%- set tooltip = "You are seeing a pre-release version of the {} crate. Click here to go to the latest stable version."|format(metadata.name) -%}
            {%- set title = "Go to latest stable release" -%}
        {%- else -%}
            {%- set tooltip = "You are seeing an outdated version of the {} crate. Click here to go to the latest version."|format(metadata.name) -%}
            {%- set title = "Go to latest version" -%}
        {%- endif -%}

        <li class="pure-menu-item">
            <a href="{% if latest_path is defined %}{{ latest_path|safe }}{% endif %}" class="pure-menu-link warn"
                data-fragment="retain"
                title="{{ tooltip }}">
                {{ crate::icons::IconTriangleExclamation.render_solid(false, false, "") }}
                <span class="title">{{ title }}</span>
            </a>
        </li>
    {%- endif -%}

    {#- Display the platforms that the release has been built for -#}
    {%- if let Some(doc_targets) = metadata.doc_targets -%}
        {%- if !doc_targets.is_empty() -%}
            <li class="pure-menu-item pure-menu-has-children">
                <a href="#" class="pure-menu-link" aria-label="Platform">
                    {{ crate::icons::IconGears.render_solid(false, false, "") }}
                    <span class="title">Platform</span>
                </a>

                {# Build the dropdown list showing available targets #}
                <ul class="pure-menu-children" id="platforms" data-url="{{ params.platforms_partial_url() }}">
                    {%- if doc_targets.len() < crate::DEFAULT_MAX_TARGETS -%}
                        {%- set use_direct_platform_links = use_direct_platform_links() -%}
                        {%- include "rustdoc/platforms.html" -%}
                    {%- else -%}
                        <span class="rotate">{{ crate::icons::IconSpinner.render_solid(false, false, "") }}</span>
                    {%- endif -%}
                </ul>
            </li>
            {#- Display the features available in current build -#}
            <li class="pure-menu-item">
                <a href="{{ params.features_url() }}" title="Browse available feature flags of {{ metadata.name }}-{{ metadata.version }}" class="pure-menu-link">
                    {{ crate::icons::IconFlag.render_solid(false, false, "") }}
                    <span class="title">Feature flags</span>
                </a>
            </li>
        {% endif %}
    {% endif %}
</ul>
{%- include "header/topbar_end.html" -%}
